/*
 * @(#)FormatRegexUtil.java   1.0  2021年11月15日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.swift.dts.model.Table;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年11月15日
 */
public class FormatRegexUtil {

    public static String formatRegex(String ...value) {
        if (TypeUtil.isNull(value)) return "";
        List<String> list = new ArrayList<>();
        for (String obj : value) {
            list.add(changeFormat(TypeUtil.toString(obj)));
        }
        return String.join("|", list);
    }
    
    public static String formatRegex(List<String> value) {
        if (TypeUtil.isNull(value)) return "";
        return formatRegex(value.toArray(new String[value.size()]));
    }

    private static String changeFormat(String value) {
        value = value.replace(".", "\\.");
        value = value.replace("*", ".*");
        return value;
    }
    
    public static void main(String[] args) {
        String regex = "sys\\..*,information_schema\\..*,innodb\\..*,mysql\\..*,performance_schema\\..*,.*\\.swift_dts_log_event_table";
        String dbTable = "mysql.swift_dts_log_event_table";
        System.out.println(Pattern.matches(changeRegex(regex), dbTable));
    }
    
    public static boolean isRegex(Table table,List<String> regex) {
        String synRegex = FormatRegexUtil.formatRegex(regex);
        return isRegex(table, synRegex);
    }
    
    public static boolean isRegex(Table table,String regex) {
        String dbTable = table.getDbName().toLowerCase()+"."+table.getTableName().toLowerCase();
        regex=regex.toLowerCase();
        boolean isMatch = Pattern.matches(regex, dbTable);
        return isMatch;
    }
    //"sys\\..*,information_schema\\..*,innodb\\..*,mysql\\..*,performance_schema\\..*,.*\\.swift_dts_log_event_table";
    public static String changeRegex(String regex) {
        return regex.replace(",", "|");
    }
}
